import axios from 'axios'
import api from './api'
export default {
	getWxConfig(item) {
		var url = window.location.href.split("#")[0];
		axios.get(api.getWxShare(), {
				params: {
					companyId: item.companyId,
					url: url
				}
			})
			.then(res => {
				this.configWx(res.data.map, item);
			});
	},
	configWx(map, item) {
		wx.config({
			// debug: true,
			appId: map.appId, // 必填，公众号的唯一标识
			timestamp: map.timestamp, // 必填，生成签名的时间戳
			nonceStr: map.nonceStr, // 必填，生成签名的随机串
			signature: map.signature, // 必填，签名，见附录1
			jsApiList: [
				"checkJsApi",
				"onMenuShareTimeline",
				"onMenuShareAppMessage",
				"onMenuShareQQ",
				"onMenuShareWeibo",
				"onMenuShareQZone"
			] // 必填，需要使用的JS接口列表，所有JS接口列表见附录2
		});
		wx.ready(function() {
			wx.onMenuShareAppMessage({
				title: item.wxShareTitle == '' || item.wxShareTitle == null ? item.title : item.wxShareTitle, // 分享标题
				desc: item.wxShareDesc, // 分享描述
				link: window.location.href, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
				imgUrl:item.wxSharePic== '' ||item.wxSharePic==null ? 'http://qiyezhibo.migucloud.com/images/logo.png' :item.wxSharePic,
				success: function() {
					// 用户确认分享后执行的回调函数
				},
				cancel: function() {
					// 用户取消分享后执行的回调函数
				}
			});
			/*分享到朋友圈*/
			wx.onMenuShareTimeline({
				title: item.wxShareDesc == '' || item.wxShareDesc == null ? item.title : item.wxShareDesc, // 分享标题
				link: window.location.href, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
				imgUrl:item.wxSharePic== '' ||item.wxSharePic==null ? 'http://qiyezhibo.migucloud.com/images/logo.png' :item.wxSharePic,
				success: function() {
					// 用户确认分享后执行的回调函数
				},
				cancel: function() {
					// 用户取消分享后执行的回调函数
				}
			});
		});
	},
	// 获取游客信息
	isWeiXin() {
		var ua = window.navigator.userAgent.toLowerCase();
		var userid = sessionStorage.getItem("id");
		if (ua.match(/MicroMessenger/i) == "micromessenger") {
			return true;
		} else {
			localStorage.setItem("youkeId", this.hashCode());
			localStorage.setItem("youkeName", "游客" + this.fourNum().slice(9));
			localStorage.setItem("youkeImg", "http://qiyezhibo.migucloud.com/images/defaultHead.png");
			return false;
		}
	},
	hashCode() {
		let navigator = window.navigator;
		//浏览器的正式名称,浏览器的版本号,浏览器正在运行的操作系统平台
		let str =
			navigator.appName +
			navigator.appVersion +
			navigator.platform +
			returnCitySN["cip"];
		sessionStorage.setItem("cip", returnCitySN["cip"]);
		var h = 0;
		for (var i = 0; i < str.length; i++) {
			h = 31 * h + str.charCodeAt(i);
			if (h > 2147483647) h %= 2147483648; //java int溢出则取模
		}
		return h;
	},
	fourNum() {
		/*时间戳+四位随机数组成游客ID*/
		let num = Math.round(Math.random() * 9999);
		if (num < 10) {
			num = "000" + num;
		} else if (num < 100) {
			num = "00" + num;
		} else if (num < 1000) {
			num = "0" + num;
		} else {
			num = num + "";
		}
		return new Date().getTime() + num;
	},
}
